<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Area;
use app\common\model\BillPayments;
use app\common\model\FormItem;
use app\common\model\FormSubmit;
use app\common\model\FormSubmitDetail;
use app\common\model\LiveApply;
use app\common\model\LiveChat;
use app\common\model\LiveFollow;
use app\common\model\LiveView;
use app\common\model\LiveZans;
use app\common\model\Products;
use app\common\model\Room;
use app\common\model\UserToken;
use app\common\model\Form as FormModel;
use think\Db;

/***
 * 直播接口
 */
class Live extends Api
{
    /**
     * 获取直播间类目
     */
    public function getRoomCat(){
        $sort=new \app\common\model\RoomCat();
        $list=$sort->getList();
        return returnData(1,'success',$list);
    }
  
    /**
     * 我的直播视频
     */
    public function myLiveList(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $data=[
            'uid'=>$user_id,
            'page'=>input('page',1),
            'limit'=>input('limit',20)
        ];
        $live=new \app\common\model\Live();
        $list=$live->myLive($data);
        return returnData(1,'success',$list);
    }
    /**
     * 我的直播间
     */
    public function myRoom(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }

        $data=[
            'uid'=>$user_id
        ];
        $room=new Room();
        $info=$room->roomInfo($data);
        return returnData(1,'success',$info);
    }
    /**
     * 提交聊天
     */
    public function sendChat(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $room_id=input('room_id',0);
        if(!$room_id){
            return returnData(0,'获取失败');
        }
        $live_id=input('live_id',0);
        if(!$live_id){
            return returnData(0,'获取失败');
        }
        $content=input('content','','trim');
        if(!$content){
            return returnData(0,'获取失败');
        }
        $type=input('type',0);
        if(!$type){
            return returnData(0,'获取失败');
        }
        $data=[
            'type'=>$type,
            'room_id'=>$room_id,
            'content'=>$content,
            'live_id'=>$live_id,
            'uid'=>$user_id
        ];
        $chat=new LiveChat();
        list($res,$msg)=$chat->addChat($data);
        if(!$res){
            return returnData(0,$msg);
        }
        return returnData(1,'success');
    }
    /**
     * 聊天消息列表
     */
    public function chatList(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $room_id=input('room_id',0);
        if(!$room_id){
            return returnData(0,'获取失败');
        }
        $live_id=input('live_id',0);
        if(!$live_id){
            return returnData(0,'获取失败');
        }
        $page=input('page',1);
        $limit=input('limit',20);
        $data=[
            'page'=>$page,
            'limit'=>$limit,
            'room_id'=>$room_id,
            'live_id'=>$live_id,
            'uid'=>$user_id
        ];
        $chatlist=new LiveChat();
        $list=$chatlist->chatList($data);
        $list=$list->toArray();
        $item=[
            'id'=>0,
            'type'=>2,
            'room_id'=>$room_id,
            'live_id'=>$live_id,
            'content'=>getSetting('live_msg'),
            'uid'=>0
        ];
        array_unshift($list,$item);
        return returnData(1,'success',$list);
    }
    /**
     * 关注主播
     */
    public function onFollow(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $f_uid=input('f_uid',0);
        if(!$f_uid){
            return returnData(0,'关注失败');
        }
        $room_id=input('room_id',0);
//        if(!$room_id){
//            return returnData(0,'关注失败');
//        }
        $live_id=input('live_id',0);
//        if(!$live_id){
//            return returnData(0,'关注失败');
//        }
        $type=input('type',0);
        if(!$type){
            return returnData(0,'关注失败');
        }
        $data=[
            'f_uid'=>$f_uid,
            'room_id'=>$room_id,
            'live_id'=>$live_id,
            'uid'=>$user_id,
            'type'=>$type
        ];
        $follow=new LiveFollow();
        list($res,$info)=$follow->onFollow($data);
        if(!$res){
            return returnData(0,$info);
        }
        return returnData(1,'success',['follow'=>$info]);
    }
    /**
     * 点赞
     */
    public function onZans(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $room_id=input('room_id',0);
        if(!$room_id){
            return returnData(0,'点赞失败');
        }
        $live_id=input('live_id',0);
        if(!$live_id){
            return returnData(0,'点赞失败');
        }
        $data=[
            'uid'=>$user_id,
            'room_id'=>$room_id,
            'live_id'=>$live_id
        ];
        $zans=new LiveZans();
        list($res,$msg)=$zans->onZans($data);
        if(!$res){
            return returnData(0,$msg);
        }
        return returnData(1,'success');
    }
    /**
     * 退出直播间
     */
    public function offRoom(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $room_id=input('room_id',0);
        if (!$room_id) {
            return returnData(0,'操作失败房间');
        }
        $live_id=input('live_id',0);
        if (!$live_id) {
            return returnData(0,'操作失败直播');
        }
        $data=[
            'uid'=>$user_id,
            'room_id'=>$room_id,
            'live_id'=>$live_id
        ];
        $view=new LiveView();
        list($res,$msg)=$view->offView($data);
        if(!$res){
            return returnData(0,$msg);
        }
        return returnData(1,'success');
    }
    /**
     * 进入直播间
     */
    public function enterRoom(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $room_id=input('room_id',0);
        if (!$room_id) {
            return returnData(0,'操作失败房间');
        }
        $live_id=input('live_id',0);
        if (!$live_id) {
            return returnData(0,'操作失败直播');
        }
        $data=[
            'uid'=>$user_id,
            'room_id'=>$room_id,
            'live_id'=>$live_id
        ];
        $view=new LiveView();
        list($res,$msg)=$view->onView($data);
        if(!$res){
            return returnData(0,$msg);
        }
        return returnData(1,'success');
    }
    /**
     * 查看视频
     */
    public function liveView(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        $userTokenModel = new UserToken();
        $result         = $userTokenModel->checkToken($token);
        if (!$result['status']) {
            return error_code(14007);
        } else {
            $user_id = $result['data']['user_id'];
        }
        $data=[
            'id'=>input('id',0),
            'uid'=>$user_id
        ];
        $room=new \app\common\model\Room();
        $view=$room->liveView($data);
        return returnData(1,'success',$view);
    }

    /**
     * 直播视频列表
     * @return array|\Illuminate\Config\Repository|mixed
     */
    public function liveList(){
        $data=[
            'type'=>input('type',1),
            'page'=>input('page',1),
            'limit'=>input('limit',20),
            'adcode'=>input('adcode',0)
        ];
        $room=new \app\common\model\Room();
        $list=$room->liveList($data);
        return returnData(1,'success',$list);
    }

    /**
     * 添加宝贝到橱窗或移除
     * @return array|\Illuminate\Config\Repository|mixed
     */
    public function onMyGoods(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $data=[
            'uid'=>$user_id,
            'id'=>input('id',0)
        ];
        $goods=new \app\common\model\RoomGoods();
        list($res,$msg)=$goods->doneGoods($data);
        if(!$res){
            return returnData(0,$msg);
        }
        return returnData(1,$msg);
    }
    /**
     * 我的宝贝橱窗列表
     */
    public function myGoodsList(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $data=[
            'uid'=>$user_id,
            'key'=>input('key',''),
            'type'=>input('type',1),
            'page'=>input('page',1),
            'limit'=>input('limit',20),
        ];
        $goods=new \app\common\model\RoomGoods();
        $list=$goods->spreadGoods($data);
        return returnData(1,'success',$list);
    }

    /**
     * 添加直播宝贝列表或移除
     * @return array|\Illuminate\Config\Repository|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function onGoods(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $data=[
            'uid'=>$user_id,
            'id'=>input('id',0),
            'rg_id'=>input('rg_id',0)
        ];
        $goods=new \app\common\model\LiveGoods();
        list($res,$msg)=$goods->doneGoods($data);
        if(!$res){
            return returnData(0,$msg);
        }
        return returnData(1,$msg);
    }
    /**
     * 我的直播宝贝列表
     */
    public function goodsList(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $data=[
            'uid'=>$user_id,
            'key'=>input('key',''),
            'page'=>input('page',1),
            'limit'=>input('limit',20)
        ];
        $goods=new \app\common\model\LiveGoods();
        $list=$goods->roomGoods($data);
        return returnData(1,'success',$list);
    }
    /**
     * 直播宝贝列表橱窗
     */
    public function goods(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $uid=input('uid',0);
        if(empty($uid)){
            $uid=$user_id;
        }
        $data=[
            'uid'=>$uid,
            'page'=>input('page',1),
            'limit'=>input('limit',20)
        ];
        $goods=new \app\common\model\LiveGoods();
        $list=$goods->goodsList($data);
        return returnData(1,'success',$list);
    }
    /**
     * 申请开通直播
     */
    public function apply(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $data=[
            'title'=>'申请开通直播功能',
            'uid'=>$user_id,
            'status'=>1,
            'ctime'=>time()
        ];
        $apply=new LiveApply();
        list($res,$msg)=$apply->createApply($data);
        if(!$res){
            return returnData(0,$msg);
        }
        return returnData(1,$msg,['passed'=>1]);
    }
    /**
     * 关闭直播
     */
    public function closeLive(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $live_id=input('live_id',0,'intval');
        $liveModel=new \app\common\model\Live();
        $data=[
            'live_id'=>$live_id,
            'uid'=>$user_id
        ];
        list($res,$info)=$liveModel->closeLive($data);
        if(!$res){
            return returnData(0,$info);
        }
        return returnData(1,'success',$info);
    }
    /**
     * 创建直播
     */
    public function createLive(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $cover=$this->request->param('cover');
        if(empty($cover)){
            return returnData(0,'请上传封面');
        }
        $adcode=input('ad_code',0,'intval');
        if(!empty($adcode)){
            $city=Area::where('id',$adcode)->find();
            if($city['depth']==2){
                $city_id=$adcode;
            }else{
                $city_id=$city['parent_id'];
            }
        }
        $room_id=input('room_id',0,'intval');
        if(empty($room_id)){
            return returnData(0,'直播创建失败');
        }
        $title=input('title','','trim');
        if(empty($title)){
            return returnData(0,'请填写直播主题');
        }
        $sortid=input('sortid',0,'intval');
        if(empty($sortid)){
            return returnData(0,'请选择直播类别');
        }
        $data=[
            'city_id'=>$city_id?$city_id:0,
            'room_id'=>$room_id,
            'sort_id'=>$sortid,
            'title'=>$title,
            'cover'=>$cover,
            'uid'=>$user_id,
            'ctime'=>time()
        ];
        $liveModel=new \app\common\model\Live();
        list($res,$live_id)=$liveModel->createLive($data);
        if(!$res){
            return returnData(0,'直播创建失败');
        }
        return returnData(1,'获取成功',['live_id'=>$live_id]);
    }
    /**
     *创建直播房间
     * @return array
     */
    public function createRoom(){
        $token = input('token','');//token值 会员登录后传
        if (!$token) {
            return returnData(0,'关键参数缺失');
        }
        $user_id = 0;
        if ($token) {
            $userTokenModel = new UserToken();
            $result         = $userTokenModel->checkToken($token);
            if (!$result['status']) {
                return error_code(14007);
            } else {
                $user_id = $result['data']['user_id'];
            }
        }
        $cover=$this->request->param('cover');
        if(empty($cover)){
            return returnData(0,'请上传封面');
        }
        $sortid=$this->request->param('sortid');
        if(empty($sortid)){
            return returnData(0,'请选择直播类别');
        }
        $adcode=input('ad_code',0,'intval');
        if(!empty($adcode)){
            $city=Area::where('id',$adcode)->find();
            if($city['depth']==2){
                $city_id=$adcode;
            }else{
                $city_id=$city['parent_id'];
            }
        }
        $data=[
            'city_id'=>$city_id?$city_id:0,
            'cover'=>$cover,
            'sort_id'=>$sortid,
            'uid'=>$user_id,
            'is_live'=>1,
            'utime'=>time()
        ];
        $roomModel=new Room();
        list($res,$room_id)=$roomModel->createRoom($data);
        if(!$res){
            return returnData(0,'房间创建失败');
        }
        return returnData(1,'获取成功',['room_id'=>$room_id]);
    }
    /**
     * 个人主页视频列表
     * @return array|\Illuminate\Config\Repository|mixed
     */
    public function videoList(){
        $data=[
            'type'=>input('type',1),
            'uid'=>input('uid',0),
            'page'=>input('page',1),
            'limit'=>input('limit',20)
        ];
        $live=new \app\common\model\Live();
        $list=$live->videoList($data);
        return returnData(1,'success',$list);
    }

}